package logx

import (
	"gitee.com/hasika/gotool/container"
	"gitee.com/hasika/gotool/logx/logdef"
	"gitee.com/hasika/gotool/logx/stdlog"
)

var _globalLog logdef.ILogger = nil
var _log logdef.ILogger = nil
var loggerMap = container.NewSyncMap[string, logdef.ILogger]()

func init() {
	SetLogger(stdlog.NewDefaultLog())
}

func SetLogger(logger logdef.ILogger) {
	_log = logger
	_globalLog = _log.WithSkip(1)
}

func Debug(args ...interface{}) {
	_globalLog.Debug(args...)
}

func Debugf(format string, args ...interface{}) {
	_globalLog.Debugf(format, args...)
}

func Info(args ...interface{}) {
	_globalLog.Info(args...)
}

func Infof(format string, args ...interface{}) {
	_globalLog.Infof(format, args...)
}

func Warn(args ...interface{}) {
	_globalLog.Warn(args...)
}

func Warnf(format string, args ...interface{}) {
	_globalLog.Warnf(format, args...)
}

func Error(args ...interface{}) {
	_globalLog.Error(args...)
}

func Errorf(format string, args ...interface{}) {
	_globalLog.Errorf(format, args...)
}

func WithField(key string, value interface{}) logdef.ILogger {
	return _log.WithField(key, value)
}

func WithFields(fields logdef.Fields) logdef.ILogger {
	return _log.WithFields(fields)
}

func WithSkip(skip int) logdef.ILogger {
	return _log.WithSkip(skip)
}

func WithName(name string) logdef.ILogger {
	return _log.WithName(name)
}

func WithLevel(level logdef.Level) logdef.ILogger {
	return _log.WithLevel(level)
}

func Logger(name ...string) logdef.ILogger {
	if len(name) != 1 {
		return _log
	}
	loggerName := name[0]
	ret, _ := loggerMap.LoadOrStoreNew(loggerName, func() logdef.ILogger {
		return _log.WithName(loggerName)
	}, nil, loggerName)
	return ret
}
